package edu.fhsu.csci468;

import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;

public class ConnectBox extends JFrame {

    private static final long serialVersionUID = 1L;
    private JDesktopPane jDesktopPane = null;
    private JTextField jTextField = null;
    private JButton jButton = null;
    private JLabel jLabel = null;
    private String ip;

    public void closeWindow()
    {
    	this.dispose();
    }
    
    /**
     * This is the default constructor
     */
    public ConnectBox() {
        super();
        this.setLocationRelativeTo(null);
        initialize();
        
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJDesktopPane());
        this.setTitle("Connect To Server");
    }

    /**
     * This method initializes jDesktopPane    
     *     
     * @return javax.swing.JDesktopPane    
     */
    private JDesktopPane getJDesktopPane() {
        if (jDesktopPane == null) {
            jLabel = new JLabel();
            jLabel.setBounds (new Rectangle(45, 15, 121, 31));
            jLabel.setText("Enter IP Address");
            jDesktopPane = new JDesktopPane();
            jDesktopPane.add(getJTextField(), null);
            jDesktopPane.add(getJButton(), null);
            jDesktopPane.add(jLabel, null);
        }
        return jDesktopPane;
    }

    /**
     * This method initializes jTextField    
     *     
     * @return javax.swing.JTextField     
     */
    private JTextField getJTextField() {
        if (jTextField == null) {
            jTextField = new JTextField("127.0.0.1");
            jTextField.setBounds (new Rectangle(45, 45, 212, 31));
        }
        return jTextField;
    }

    /**
     * This method initializes jButton    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.addActionListener(new Connect());
            jButton.setBounds(new Rectangle(60, 89, 151, 32));
            jButton.setText("Connect");
        }
        return jButton;
    }

    private class Connect implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		ip = jTextField.getText();
    		ControlPanelClient cpc = new ControlPanelClient(ip);
    		new Thread((cpc)).start();
    		closeWindow();

    	}
    }
    
}  //  @jve:decl-index=0:visual-constraint="249,43"